package cn.kgc.springcloud.service.serviceimpl;

import cn.kgc.springcloud.client.CityClient;
import cn.kgc.springcloud.client.ClazzClient;
import cn.kgc.springcloud.client.GroupClient;
import cn.kgc.springcloud.client.TagClient;
import cn.kgc.springcloud.dao.StudentDao;
import cn.kgc.springcloud.dao.StudentTagDao;
import cn.kgc.springcloud.entity.*;
import cn.kgc.springcloud.service.StudentService;
import cn.kgc.springcloud.util.DateUtil;
import cn.kgc.springcloud.vo.StudentRequestVo;
import cn.kgc.springcloud.vo.StudentResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private CityClient cityClient;
    @Autowired
    private ClazzClient clazzClient;
    @Autowired
    private GroupClient groupClient;
    @Autowired
    private TagClient tagClient;
    @Autowired
    private StudentTagDao studentTagDao;
    @Override
    public List<StudentResponseVo> queryAll() {
        //创建集合 存放返回的数据
        ArrayList<StudentResponseVo> studentResponseVos = new ArrayList<>();
        List<Student> students=studentDao.queryAll();
        //遍历集合 获取 城市 班级 小组的id
        students.forEach(student -> {
            //将student对象 转换为studentVo对象
            StudentResponseVo studentResponseVo = new StudentResponseVo();
            City city = cityClient.selectOne(Integer.parseInt(student.getCityId()));
            studentResponseVo.setCity(city);
            Clazz clazz = clazzClient.getOne(Integer.parseInt(student.getClazzId()));
            studentResponseVo.setClazz(clazz);
            Group group = groupClient.selectOne(Integer.parseInt(student.getGroupId()));
            studentResponseVo.setGroup(group);
            //查询标签对象 学生 标签 多对多 1.根据学生id 查询学生对应 tid 2.根据tid 查询标签信息
            List<StudentTag> studentTags = studentTagDao.getOne(student.getId());
            studentTags.forEach(studentTag -> {
                Tag tag = tagClient.getOne(studentTag.getTid());
                studentResponseVo.setTag(tag);
            });
            BeanUtils.copyProperties(student, studentResponseVo);
            System.out.println("studentVo = " + studentResponseVo);
            //添加studentVo到集合
            studentResponseVos.add(studentResponseVo);
        });
        return studentResponseVos;
    }

    @Override
    public StudentResponseVo insert(StudentRequestVo studentRequestVo) {
        //将StudentRequestVo -> Student
        Student student = new Student();
        BeanUtils.copyProperties(studentRequestVo,student);
        //通过工具类 计算学生的年龄 生肖 星座
        student.setAge(DateUtil.getAge(student.getBir()));
        student.setAttr(DateUtil.getYear(student.getBir()));
        student.setStarts(DateUtil.getConstellation(student.getBir()));
        studentDao.addStudent(student);
        //student对象中已经包含自增主键id值
        Integer sid = student.getId();
        List<Integer> tagIds = studentRequestVo.getTagIds();
        //遍历集合 创建StudentTag对象 StudentTag表
        tagIds.forEach(tid->{
            studentTagDao.addStudentTag(new StudentTag(sid,tid));
        });
        //将student对象 -> StudentResponseVo
        StudentResponseVo studentResponseVo = new StudentResponseVo();
        BeanUtils.copyProperties(student,studentResponseVo);
        City city = cityClient.selectOne(Integer.parseInt(student.getCityId()));
        studentResponseVo.setCity(city);
        Clazz clazz = clazzClient.getOne(Integer.parseInt(student.getClazzId()));
        studentResponseVo.setClazz(clazz);
        Group group = groupClient.selectOne(Integer.parseInt(student.getGroupId()));
        studentResponseVo.setGroup(group);
        //遍历标签集合 调用标签微服务 查询标签id对应的对象
        tagIds.forEach(tid->{
            studentResponseVo.getTags().add(tagClient.getOne(tid));
        });
        return studentResponseVo;
    }
}
